/**
 * 图片旋转 
 * @param {Object} angle
 * @param {Object} whence
 */


jQuery.fn.rotate = function (angle,minlong, whence) {
    var p = this.get(0);
    console.log('33333333==='+p.height+','+ p.width);
    // we store the angle inside the image tag for persistence  
    if (!whence) {
        p.angle = ((p.angle == undefined ? 0 : p.angle) + angle) % 360;
    } else {
        p.angle = angle;
    }
 
    if (p.angle >= 0) {
        var rotation = Math.PI * p.angle / 180;
    } else {
        var rotation = Math.PI * (360 + p.angle) / 180;
    }
    var costheta = Math.round(Math.cos(rotation) * 1000) / 1000;
    var sintheta = Math.round(Math.sin(rotation) * 1000) / 1000;
    //alert(costheta+","+sintheta);  
 
    if (document.all && !window.opera) {
        var canvas = document.createElement('img');
 
        canvas.src = p.src;
        canvas.height = p.height;
        canvas.width = p.width;
 
        canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11=" + costheta + ",M12=" + (-sintheta) + ",M21=" + sintheta + ",M22=" + costheta + ",SizingMethod='auto expand')";
    } else {
        //var canvas = document.createElement('canvas');
        
        var canvas = document.getElementById('canvas_'+p.id);
        if (!p.oImage) {
                canvas.oImage = new Image();
                canvas.oImage.src = p.src;
        } else {
            canvas.oImage = p.oImage;
        }
        var canvas_oImage_width = canvas.oImage.width ;
        var canvas_oImage_height = canvas.oImage.height ;
        console.log('2222222222222==='+p.id+'===='+canvas_oImage_height+','+canvas_oImage_height+','+minlong);
        if(canvas_oImage_width>=canvas_oImage_height){
            if(canvas_oImage_width>=minlong){
                canvas_oImage_height = canvas_oImage_height * (minlong/canvas_oImage_width);
                canvas_oImage_width = minlong;
            }
        }else{
            if(canvas_oImage_height > minlong){
                canvas_oImage_width = canvas_oImage_width * (minlong/canvas_oImage_height);
                canvas_oImage_height = minlong;
            }
        }
        canvas.width = Math.abs(costheta * canvas_oImage_width) + Math.abs(sintheta * canvas_oImage_height);
        canvas.height = Math.abs(costheta * canvas_oImage_height) + Math.abs(sintheta * canvas_oImage_width)
        console.log('1111111111111==='+p.id+'===='+canvas.width+','+canvas.height+','+minlong);
        canvas.style.width = canvas.width ;
        canvas.style.height = canvas.height;
        var context = canvas.getContext('2d');
        context.save();
        if (rotation <= Math.PI / 2) {
            context.translate(sintheta * canvas_oImage_height, 0);
        } else if (rotation <= Math.PI) {
            context.translate(canvas.width, -costheta * canvas_oImage_height);
        } else if (rotation <= 1.5 * Math.PI) {
            context.translate(-costheta * canvas_oImage_width, canvas.height);
        } else {
            context.translate(0, -sintheta * canvas_oImage_width);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, canvas_oImage_width, canvas_oImage_height);
        context.restore();
    }
    //canvas.id = p.id;
    canvas.angle = p.angle;
    //p.parentNode.replaceChild(canvas, p);
}
 
jQuery.fn.rotateRight = function (angle,minlong) {
    this.rotate(angle == undefined ? 90 : angle,minlong);
}
 
jQuery.fn.rotateLeft = function (angle,minlong) {
    this.rotate(angle == undefined ? -90 : -angle,minlong);
}